package com.xp.tinyurl.service.thirdprovider.vo;

import com.xp.tinyurl.common.utils.BinaryUtil;

/**
 * @author  (xp)
 **/
public class UpushShorterUrlResult {
    public static final long NOT_VALID = -1;
    private boolean success;
    private long uuid;
    private long permutedId;

    public UpushShorterUrlResult(boolean success, long uuid, long permutedId) {
        this.success = success;
        this.uuid = uuid;
        this.permutedId = permutedId;
    }

    public static UpushShorterUrlResult fail() {
        return fail(NOT_VALID, NOT_VALID);
    }

    public static UpushShorterUrlResult fail(long uuid, long permutedId) {
        return new UpushShorterUrlResult(false, uuid, permutedId);
    }

    public static UpushShorterUrlResult success(long uuid, long permutedId) {
        return new UpushShorterUrlResult(true, uuid, permutedId);
    }

    public long getUuid() {
        return uuid;
    }

    /**
     * 仅当需要的时候转换成shortUrl
     */
    public String getShortUrl() {
        if (permutedId <= NOT_VALID) {
            throw new IllegalStateException("permutedId is invalid");
        }
        return BinaryUtil.convertDecimalTo62(permutedId);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}
